package n121;

public class Solution {
    public static void main(String[] args) {
        System.out.println(new Solution().maxProfit(new int[]{}));
    }
    public int maxProfitV1(int[] prices) {
        int max = 0;

        for (int i = 0; i < prices.length; i++) {
            for (int j = i; j < prices.length; j++) {
                if (i==j){
                    continue;
                }
                int diff = prices[j] - prices[i];
                if (diff > max) {
                    max = diff;
                }
            }
        }

        return max;
    }

    public int maxProfit(int[] prices) {
        int max = 0;
        int min = prices[0];

        for (int i = 1; i < prices.length; i++) {
            if (prices[i] < min){
                min = prices[i];
            }else {
                max = Math.max(max,prices[i]-min);
            }
        }

        return max;
    }
}
